package com.example.mvvm;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.databinding.ObservableField;
import android.widget.TextView;

import com.example.mvvm.BR;

/**
 * Created by xzh on 2018/3/3.
 * 邮箱 ：347649749@qq.com
 * 版本 ： ver 1.0
 * 功能 ：
 */

public class UserModel extends BaseObservable {

    private String username;
    private String password;
    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> pass = new ObservableField<>();
    public static ObservableField<String> newName = new ObservableField<>();

    public UserModel() {
    }

    public UserModel(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @BindingAdapter({"newName"})
    public static void setNewVal(TextView tv ,ObservableField<String> newNameStr){
        String str = newNameStr.get();
        tv.setText(str);
    }

    /*@Bindable
    public ObservableField<String> getName() {
        return name;
    }

    public void setName(ObservableField<String> name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
    @Bindable
    public ObservableField<String> getPass() {
        return pass;
    }

    public void setPass(ObservableField<String> pass) {
        this.pass = pass;
        notifyPropertyChanged(BR.pass);
    }*/

    @Bindable
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        notifyPropertyChanged(BR.username);
    }

    @Bindable
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        notifyPropertyChanged(BR.password);
    }

    public int checkUserValidity(String usernameStr,String passwordStr){
        if (usernameStr == null || passwordStr == null || usernameStr.isEmpty()||passwordStr.isEmpty()){
            return -2;
        }else {
            if (username == null || password == null || username.isEmpty() || password.isEmpty()){
                return -3;
            }else {
                if (username.equals(usernameStr) && password.equals(passwordStr)) {
                    return 1;
                } else {
                    return -1;
                }
            }
        }
    }

    @Override
    public String toString() {
        return "UserModel{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
